package com.sxyjhh.dbutil.basic;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/22.
 */
public class DBCPManager {

    private Map<String, CustomDataSource> customDataSourceMap = new HashMap<String, CustomDataSource>();

    public static DBCPManager instance;

    private DBCPManager() {
    }

    public static DBCPManager getInstance() {
        if (instance == null) {
            instance = new DBCPManager();
        }
        return instance;
    }

    public CustomDataSource getCustomDataSource(String driverName, String dbUrl, String userName, String password, int connPollSize) {
        CustomDataSource customDataSource = null;
        String dbPoolKey = dbUrl+"_"+userName+"_"+password;
        if (customDataSourceMap.containsKey(dbPoolKey)) {
            customDataSource = customDataSourceMap.get(dbPoolKey);
        }else{
            customDataSource = new CustomDataSource(driverName, dbUrl, userName, password, connPollSize);
            customDataSourceMap.put(dbPoolKey,customDataSource);
        }
        return customDataSource;
    }

    public void removeDataSource(String dbUrl, String userName, String password) throws SQLException {
        String dbPoolKey = dbUrl+"_"+userName+"_"+password;
        if (customDataSourceMap.containsKey(dbPoolKey)) {
            customDataSourceMap.get(dbPoolKey).closeDataSource();
            customDataSourceMap.remove(dbPoolKey);
        }
    }

}
